<template>
  <el-dialog
    @close="reset"
    :title="roleData.id > 0 ? '编辑' : '新增'"
    :visible.sync="info.isShow"
  >
    <el-form ref="roleForm" :model="roleData" label-width="80px" :rules="rules">
      <el-form-item label="角色名称" prop="rolename">
        <el-input placeholder="角色名称" v-model="roleData.rolename"></el-input>
      </el-form-item>
      <el-form-item label="角色权限" prop="menus">
        <!-- 
              data:数据源 
              show-checkbox: 显示复选框
              default-expanded-keys: 指定需要展开的节点
              default-checked-keys:默认需要被勾选的节点
              default-expand-all: 展开所有节点
          -->
        <el-tree
          :data="menuList"
          show-checkbox
          node-key="id"
          default-expand-all
          :props="defaultProps"
          ref="tree"
          :default-checked-keys="checkedKeys"
          :check-strictly="true"
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="角色状态" prop="status">
        <el-radio-group v-model="roleData.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  computed: {
    ...mapState("menu", ["menuList"]),
  },
  props: {
    info: Object,
  },
  data() {
    // 自定义表单校验函数
    const validMenus = (rule, value, callback) => {
      if (this.$refs.tree.getCheckedKeys().length === 0) {
        return callback(new Error("请选择角色权限"));
      }
      callback();
    };

    return {
      // 属性控件默认勾选项
      checkedKeys: [],
      // 和表单元素进行绑定的数据
      roleData: {
        rolename: "", //角色名称
        menus: "", // 角色权限
        status: "", // 角色状态
      },
      // 表单校验规则对象
      rules: {
        rolename: {
          required: true,
          message: "请输入角色名称",
          trigger: "blur",
        },
        status: { required: true, message: "请选择角色状态", trigger: "blur" },
        menus: { validator: validMenus, trigger: "blur" },
      },

      defaultProps: {
        children: "children", // 存放子节点的属性
        label: "title", // 存放节点名称的属性
      },
    };
  },
  methods: {
    ...mapActions("menu", ["getMenuListAction"]),
    ...mapActions("role", ["getRoleListAction"]),
    // 更新和表单绑定的数据
    setFormData(obj) {
      this.roleData = { ...obj };
      this.checkedKeys = this.roleData.menus.split(",");
    },
    //  数据重置
    reset() {
      // 1-关闭对话框组件
      this.info.isShow = false;
      //   重置表单
      this.roleData = {
        rolename: "",
        menus: "",
        status: "",
        id: "",
      };
      // 手动重置属性控件默认勾选项
      this.$refs.tree.setCheckedKeys([]);
    },
    // 提交表单
    submit() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          // 手动收集属性控件中用户勾选的节点数据
          // this.roleData.menus = this.$refs.tree.getCheckedKeys().join(",");
          const menus = [];
          this.$refs.tree.getCheckedNodes().forEach((item) => {
            menus.push(item.id);
            if (!menus.includes(item.pid)) {
              menus.push(item.pid);
            }
          });
          this.roleData.menus = menus.join(',');
          const url=this.roleData.id>0?'/roleedit':'/roleadd'
          this.$axios.post(url, this.roleData).then((res) => {
            if (res.code === 200) {
              // 更新角色列表
              this.getRoleListAction();
              // 重置表单数据&关闭对话框组件
              this.reset();
              // 成功提示消息
              this.$message.success(res.msg);
            }else{
               this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
  created() {
    // 请求菜单列表
    this.getMenuListAction();
  },
};
</script>

<style>
</style>